<?php

namespace app\admin\controller;

use think\Cache;
use think\cache\driver\Redis;
use think\Controller;

class Choujiang extends Controller
{
    //重置奖池
    public function luckyRest()
    {
        $data = [
            1 => 1,
            2 => 3,
            3 => 6,
            4 => 10,
        ];


        $max = array_sum($data);

        foreach ($data as $key => $val) {


            $win = mt_rand(1, $max);


            if ($win <= $val) {
               if ($key==1)
               {
                   return "一等奖".$key;
               }
                if ($key==2)
                {
                    return "二等奖".$key;
                }
                if ($key==3)
                {
                    return "三等奖".$key;
                }
                if ($key==4)
                {
                    return "四等奖".$key;
                }
            } else {
                $max -= $val;

            }

        }


    }


public function luck()
{
            $redis = new Redis();
        $res1 = $redis->sadd('lucky', '一等奖');

        for ($i = 1; $i <= 3; $i++) {
            $res2 = $redis->sadd('lucky', '二等奖' . $i);
        }
        for ($i = 1; $i <= 6; $i++) {
            $res3 = $redis->sadd('lucky', '三等奖' . $i);
        }
        for ($i = 1; $i <= 10; $i++) {
            $res4 = $redis->sadd('lucky', '谢谢参与' . $i);
        }

        if ($res1 == 1 && $res2 == 1 && $res3 == 1 && $res4 == 1) {
            return json(['code' => 200, 'msg' => '重置成功', 'data' => '']);
        } else {
            return json(['code' => 400, 'msg' => '网络错误', 'data' => '']);
        }

}


//进行抽奖
    public function luckDo()
    {


        $redis = new Redis();
        $res = $redis->spop('lucky');
        if ($res) {
            return json(['code' => 200, 'msg' => '抽奖成功', 'data' => $res]);
        } else {
            return json(['code' => 400, 'msg' => '请重置奖池', 'data' => '']);

        }
    }
}
